package com.sfpay.coreplatform.member.service.validate;

import com.sfpay.coreplatform.member.common.MemberResponseCode;
import com.sfpay.coreplatform.member.common.constant.Constants;
import com.sfpay.coreplatform.member.common.util.StringUtil;
import com.sfpay.coreplatform.member.service.exception.MemberServiceException;

/**
 * 
 * 
 * 类说明：公共业务校验
 *  
 * 
 * <p>
 * 详细描述：
 *   
 * 
 * @author 313717
 *   
 * CreateDate: 2013-4-12
 */
public class ValidateUtil 
{
	/**
	 * 
	 *
	 * 方法说明：密码是否为空与格式检查
	 * 
	 * @param pwd
	 * @throws MemberServiceException 
	 */
	public static void checkPwd(String pwd) throws MemberServiceException
	{
		if(StringUtil.isStrNull(pwd))
		{
			throw new MemberServiceException(MemberResponseCode.MEMBER_COMMON_PARAM_ISNULL,"the Parameter pwd can not be null");
		}
		else if(!CommonCheckUtil.checkPwd(pwd))
		{
			throw new MemberServiceException(MemberResponseCode.INVALID_PWD,"the Parameter pwd format  is incorrect");
		}
	}
	
	/**
	 * 
	 *
	 * 方法说明： 邮箱是否为空与格式检查
	 * 
	 * @param email
	 * @throws MemberServiceException 
	 */
	public static void checkEmail(String email) throws MemberServiceException
	{
		if(StringUtil.isStrNull(email))
		{
			throw new MemberServiceException(MemberResponseCode.MEMBER_COMMON_PARAM_ISNULL,"the Parameter email can not be null");
		}
		else if(!CommonCheckUtil.checkMail(email))
		{
			throw new MemberServiceException(MemberResponseCode.INVALID_MAIL,"the Parameter email is invalid");
		}
	}
	
	/**
	 * 
	 *
	 * 方法说明： 手机是否为空与格式检查
	 * 
	 * @param email
	 * @throws MemberServiceException 
	 */
	public static void checkMobile(String mobile) throws MemberServiceException
	{
		if(StringUtil.isStrNull(mobile))
		{
			throw new MemberServiceException(MemberResponseCode.MEMBER_COMMON_PARAM_ISNULL,"the Parameter mobile can not be null");
		}
		else if(!CommonCheckUtil.checkMobile(mobile))
		{
			throw new MemberServiceException(MemberResponseCode.INVALID_MOBILE,"the Parameter mobile is invalid");
		}
	}
	
	/**
	 * 
	 *
	 * 方法说明：会员号校验
	 *
	 * @param memberNo	会员号
	 * @throws MemberServiceException
	 */
	public static void checkMemberNo(Long memberNo) throws MemberServiceException
	{
		if(!CommonCheckUtil.checkMemberNo(memberNo))
		{
			throw new MemberServiceException(MemberResponseCode.INVALID_MEMBER_NO,"the Parameter memberNo is invalid");
		}
	}
	
	
	public static void checkParamIsNull(String str,String paramName) throws MemberServiceException
	{
		if(StringUtil.isStrNull(str))
		{
			throw new MemberServiceException(MemberResponseCode.MEMBER_COMMON_PARAM_ISNULL,"the Parameter " + paramName + " can not be null");
		}
	}
	
	public static void checkObjectParamIsNull(Object str,String paramName) throws MemberServiceException
	{
		if(StringUtil.isObjectNull(str))
		{
			throw new MemberServiceException(MemberResponseCode.MEMBER_COMMON_PARAM_ISNULL,"the Parameter " + paramName + " can not be null");
		}
	}
	
	/**
	 * 
	 *
	 * 方法说明： 对分页参数的校验
	 * 
	 * @param pageNo 第几页
	 * @param pageSize 每页大小.为0的时候,查询全部信息
	 * @throws MemberServiceException 
	 */
	public static void checkPageParam(int pageNo, int pageSize) throws MemberServiceException
	{
		if(pageNo <= 0)
		{
			throw new MemberServiceException(MemberResponseCode.INVALID_PAGENO,"the num of page is error");
		}
		else if(pageSize <= 0)
		{
			throw new MemberServiceException(MemberResponseCode.INVALID_PAGESIZE,"the size of per page error");
		}
	}
	
	public static void checkLoginType(String loginId, int loginType) throws MemberServiceException
	{
		if(Constants.MOBILE_FLAG == loginType)
		{
			checkMobile(loginId);
		}
		else if(Constants.MAIL_FLAG == loginType)
		{
			checkEmail(loginId);
		}
		else if(Constants.LOGINNAME_FLAG == loginType)
		{
			checkParamIsNull(loginId, "loginName");
		}
		else
		{
			throw new MemberServiceException(MemberResponseCode.INVALID_LOGIN_TYPE, "the login type is wrong");
		}
	}
}
